#!/usr/bin/env sh

STAGE3_URL=https://mirrors.aliyun.com/gentoo/releases/amd64/autobuilds/current-install-amd64-minimal/stage3-amd64-openrc-20210912T170541Z.tar.xz
CHROOT_MODE=0
step__format_disk() {
    echo "::set date:"
    ntpdate ntp.aliyun.com

    echo "::format disk"
    cfdisk /dev/sda

    mkfs.ext2 /dev/sda1
    mkswap /dev/sda2
    mkfs.ext4 /dev/sda3

    echo "::mount /root:"
    mkdir -p /mnt/gentoo
    mount /dev/sda3 /mnt/gentoo
    swapon /dev/sda2
}

step__edit_fstab() {
    blkid
    sleep 3
    fdisk -l
    sleep 3
    nano -w /mnt/gentoo/etc/fstab
}

step__unzip_stage3() {
    echo "::install stage3:"
    cd /mnt/gentoo/
    wget ${STAGE3_URL}
    tar xpvf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner

    echo "::config portage:"
    mkdir --parents /mnt/gentoo/etc/portage/repos.conf
    cp /mnt/gentoo/usr/share/portage/config/repos.conf /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
    cp --dereference /etc/resolv.conf /mnt/gentoo/etc/

    echo 'MAKEOPTS="-j4"' >> /mnt/gentoo/etc/portage/make.conf
    echo 'FEATURES="buildpkg getbinpkg"' >> /mnt/gentoo/etc/portage/make.conf
    echo '#PORTAGE_BINHOST="http://binhost.com/packages"' >> /mnt/gentoo/etc/portage/make.conf
    echo 'ACCEPT_LICENSE="*"' >> /mnt/gentoo/etc/portage/make.conf
    echo 'I10N="en-US zh-CN en zh"' >> /mnt/gentoo/etc/portage/make.conf
    echo 'LINGUAS="en-US zh-CN en zh"' >> /mnt/gentoo/etc/portage/make.conf

    echo "::select china mirrors:"
    mirrorselect -i -o >> /mnt/gentoo/etc/portage/make.conf

    echo "::config \$USE:"
    nano -w /etc/portage/make.conf
    emerge --info | grep ^USE
}

step__chroot() {
    echo "::chroot to new root:"
    mount --types proc /proc /mnt/gentoo/proc
    mount --rbind /sys /mnt/gentoo/sys
    mount --make-rslave /mnt/gentoo/sys
    mount --rbind /dev /mnt/gentoo/dev
    mount --make-rslave /mnt/gentoo/dev 

    chroot /mnt/gentoo /bin/bash
    source /etc/profile
    export PS1="(chroot) ${PS1}"

    echo "::sync portage:"
    emerge-webrsync
    emerge --sync
}

step__set_locale() {
    echo "set timezone:"
    echo "Asia/Shanghai" > /etc/timezone
    emerge --config sys-libs/timezone-data

    locale -a
    echo "::config locale:"
    nano -w /etc/locale.gen
    locale-gen

    eselect locale list
    echo "::eselect locale set n"

    echo "::set date:"
    ntpdate ntp.aliyun.com

    reload_env
}

reload_env() {
    echo "::reload env:"
    env-update && source /etc/profile && export PS1="(chroot) ${PS1}"
}

step__install_kernel() {
    echo "::kernel install:"
    # echo "::install kernel souces:"
    # emerge --ask sys-kernel/gentoo-sources
    # ls -l /usr/src/linux
    # emerge --ask sys-apps/pciutils

    # echo "::gen initramfs:"
    # emerge --ask sys-kernel/genkernel
    # genkernel --install initramfs
    # genkernel --lvm --mdadm --install initram
    # ls /boot/initramfs*

    # genkernel all

    # emerge --ask sys-kernel/gentoo-kernel
    emerge --ask sys-kernel/gentoo-kernel-bin
    emerge --depclean
    ls /boot/kernel* /boot/initramfs*

    # echo "::rebuild initramfs:"
    # emerge --ask @module-rebuild
    # emerge --config sys-kernel/gentoo-kernel
    # emerge --config sys-kernel/gentoo-kernel-bin
}

step__configure_net() {
    echo "::hostname:${hostname}"
    nano -w /etc/conf.d/hostname

    echo "::domain:\${dns_domain_lo}"
    nano -w /etc/conf.d/net

    echo "::config network:"
    echo 'config_eth0="dhcp"' >> /etc/conf.d/net
    cd /etc/init.d
    ln -s net.lo net.eth0
    rc-update add net.eth0 default

    emerge --ask net-misc/dhcpcd
    emerge --ask net-dialup/ppp
    emerge --ask net-wireless/iw net-wireless/wpa_supplicant
}

step__install_tools() {
    echo "::install firmware:"
    emerge --ask sys-kernel/linux-firmware

    echo "::install log tool:"
    emerge --ask app-admin/sysklogd
    rc-update add sysklogd default

    emerge --ask sys-process/cronie
    rc-update add cronie default

    emerge --ask sudo layman
}

step__set_root_passwd() {
    echo "::set root passwd:"
    passwd
}

step__add_user() {
    echo "::add user:"
    useradd -m -G users,wheel,audio -s /bin/bash damon
    passwd damon
}

step__install_grub() {
    echo "::install grub2:"
    emerge --ask --verbose sys-boot/grub:2
    grub-install /dev/sda
    grub-mkconfig -o /boot/grub/grub.cfg
}

step__reboot() {
    echo "::reboot:"
    if [ "$CHROOT_MODE" == "0" ]; then
        exit
        cd /
        umount -l /mnt/gentoo/dev{/shm,/pts,}
        umount -R /mnt/gentoo 
    fi
    reboot
}

main() {
    if [ "$CHROOT_MODE" == "0" ]; then
        echo "    1) Format disk"
        echo "    2) Unpack stage3"
        echo "    3) Edit fstab"
        echo "    4) Chroot"
    else
        echo "    5) Set locale"
        echo "    6) Install kernel"
        echo "    7) Config network"
        echo "    8) Install tools"
        echo "    9) Set root password"
        echo "    a) Add user"
        echo "    b) Install grub"
    fi
    echo "    q) Exit && Reboot"

    read -p "please enter your choice:" item
    if [ "$CHROOT_MODE" == "0" ]; then
        case $item in
            1) step__format_disk;;
            2) step__edit_fstab;;
            3) step__unzip_stage3;;
            4) step__chroot;;
            q) step__reboot;;
            q|Q) exit;;
            *) main;;
        esac
    else
        case $item in
            5) step__set_locale;;
            6) step__install_kernel;;
            7) step__configure_net;;
            8) step__install_tools;;
            9) step__set_root_passwd;;
            a) step__add_user;;
            b) step__install_grub;;
            r) reload_env;;
            q) step__reboot;;
            *) main;;
        esac
    fi
}

